package com.example.livestockbreedingapplication.slice;

import com.example.livestockbreedingapplication.ResourceTable;
import com.example.livestockbreedingapplication.data.Result;
import com.example.livestockbreedingapplication.domain.UserData;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;

import java.util.List;

public class SystemLoginSlice extends AbilitySlice implements Component.ClickedListener {
    private Button register;
    private Button login;
    private TextField loginName;
    private TextField loginPwd;
    private Text forgetPwd;
    private Image home;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_system_login);
        home = (Image)findComponentById(ResourceTable.Id_home);
        home.setClickedListener(this);
        forgetPwd = (Text)findComponentById(ResourceTable.Id_forget_password);
        login = (Button)findComponentById(ResourceTable.Id_login);
        login.setClickedListener(this);
        register = (Button)findComponentById(ResourceTable.Id_register_user);
        register.setClickedListener(this);
        loginName =(TextField)findComponentById(ResourceTable.Id_login_phonenum);
        loginPwd = (TextField)findComponentById(ResourceTable.Id_login_password);
        forgetPwd.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        if (requestCode == 100){
            new ToastDialog(SystemLoginSlice.this)
                    .setText(resultIntent.getStringParam("change"))
                    .show();
        }
    }

    @Override
    public void onClick(Component component) {
        String url = "http://123.56.152.169:8088/userData/";
        if (component == login){
            if (loginName.getText().equals("")||loginPwd.getText().equals("")){
                new ToastDialog(this)
                        .setText("用户名或密码不能为空！")
                        .show();
            }else {
                Gson gson = new GsonBuilder().serializeNulls().create();
                ZZRHttp.get(url,new ZZRCallBack.CallBackString() {
                    @Override
                    public void onFailure(int i, String s) {
                        new ToastDialog(SystemLoginSlice.this)
                                .setText("网络不稳定")
                                .show();
                    }
                    @Override
                    public void onResponse(String s) {
                        Result result = gson.fromJson(s, Result.class);
                        if (result.getCode()==1){
                            for (JsonObject jsonObject : result.getData()){
                                UserData userData = gson.fromJson(jsonObject,UserData.class);
                                if (loginName.getText().equals(userData.getUsername()) && loginPwd.getText().equals(userData.getUserpassword())){
                                        new ToastDialog(SystemLoginSlice.this)
                                                .setText("登录成功")
                                                .show();
                                        Intent intent = new Intent();
                                        intent.setParam("username",userData.getUsername());
                                        setResult(intent);
                                        terminate();
                                }
                            }
                        }else if (result.getCode() == 0){
                            new ToastDialog(SystemLoginSlice.this)
                                    .setText("登录失败！\n用户名或密码错误！")
                                    .show();
                     }
                    }
                });
            }

        }
        if (component.getId() == ResourceTable.Id_forget_password){
            Intent intent = new Intent();
            presentForResult(new SystemChangePwdSlice(),intent,100);
        }
        if (component.getId() == ResourceTable.Id_home){
            Intent intent = new Intent();
            intent.setParam("username","未登录");
            setResult(intent);
            terminate();
        }
    }
}
